package com.quanshu.exchange.ui.gui.profile;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.profile.order_fragment.AchieveFragment;
import com.quanshu.exchange.ui.gui.profile.order_fragment.AllOrderFragment;
import com.quanshu.exchange.ui.gui.profile.order_fragment.NonPaymentFragment;
import com.quanshu.exchange.ui.gui.profile.order_fragment.NonShippedFragment;
import com.quanshu.exchange.ui.gui.profile.order_fragment.OrderAdapter;
import com.quanshu.exchange.ui.gui.profile.order_fragment.ShippedFragment;
import com.quanshu.shop.base.view.BaseTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 全部 - 我的订单
 * Created by xiniu003 on 2018/1/12.
 */

public class AllOrderActivity extends BaseActivity {

    @BindView(R.id.order_tabLayout)
    TabLayout orderTabLayout;
    @BindView(R.id.order_viewPager)
    ViewPager orderViewPager;
    @BindView(R.id.order_title)
    BaseTitleBar orderTitle;

    private List<String> titleList;//标题集合
    private List<Fragment> fragments;//页面集合
    private AllOrderFragment allOrderFragment;
    private NonPaymentFragment nonPaymentFragment;//待付款
    private NonShippedFragment nonShippedFragment;// 待发货
    private ShippedFragment shippedFragment;//已发货
    private AchieveFragment achieveFragment;//已完成
    private OrderAdapter orderAdapter;
    private int location_id;
    public static final String ORDER_ID = "order_id";
    public static final String CANCEL_ORDER_ID = "cancel_order_id";
    public static final String SPECIAL_ORDER_ID = "special_order_id";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_order);
        ButterKnife.bind(this);
        ImmersionBar.with(this).titleBar(orderTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        int order_flag = getIntent().getIntExtra("ORDER_FLAG", -1);
        if (order_flag != -1) {
            location_id = order_flag;
        }
        initView();
        doSetCurrentItem();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        titleList = new ArrayList<>();
        titleList.add("全部");
        titleList.add("待付款");
        titleList.add("待发货");
        titleList.add("待收货");
        titleList.add("已完成");

        if (allOrderFragment == null) {
            allOrderFragment = AllOrderFragment.newInstance(this);
        }

        if (nonPaymentFragment == null) {
            nonPaymentFragment = NonPaymentFragment.newInstance(this);
        }

        if (nonShippedFragment == null) {
            nonShippedFragment = NonShippedFragment.newInstance(this);
        }

        if (shippedFragment == null) {
            shippedFragment = ShippedFragment.newInstance(this);
        }

        if (achieveFragment == null) {
            achieveFragment = AchieveFragment.newInstance(this);
        }

        fragments = new ArrayList<>();
        fragments.add(allOrderFragment);
        fragments.add(nonPaymentFragment);
        fragments.add(nonShippedFragment);
        fragments.add(shippedFragment);
        fragments.add(achieveFragment);

        orderAdapter = new OrderAdapter(getSupportFragmentManager(), titleList, fragments);
        orderViewPager.setAdapter(orderAdapter);
        orderTabLayout.setupWithViewPager(orderViewPager);
    }

    private void doSetCurrentItem() {
        if (location_id == 0) {
            if (allOrderFragment == null) {
                allOrderFragment = AllOrderFragment.newInstance(this);
            }
            orderViewPager.setCurrentItem(0);
        } else if (location_id == 1) {
            if (nonPaymentFragment == null) {
                nonPaymentFragment = NonPaymentFragment.newInstance(this);
            }
            orderViewPager.setCurrentItem(1);
        } else if (location_id == 2) {
            if (nonShippedFragment == null) {
                nonShippedFragment = NonShippedFragment.newInstance(this);
            }
            orderViewPager.setCurrentItem(2);
        } else if (location_id == 3) {
            if (shippedFragment == null) {
                shippedFragment = ShippedFragment.newInstance(this);
            }
            orderViewPager.setCurrentItem(3);
        } else if (location_id == 4) {
            if (achieveFragment == null) {
                achieveFragment = AchieveFragment.newInstance(this);
            }
            orderViewPager.setCurrentItem(4);
        }
    }

    private void initListener() {
        orderTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("我的订单").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
    }

}
